package com.yingxue.lesson.service.impl;

import com.yingxue.lesson.dao.SysRolePermissionDao;
import com.yingxue.lesson.entity.SysRolePermission;
import com.yingxue.lesson.service.SysRolePermissionService;
import com.yingxue.lesson.vo.req.RolePermissionOperationReqVO;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * (SysRolePermission)表服务实现类
 *
 * @author makejava
 * @since 2022-04-05 21:41:06
 */
@Service("sysRolePermissionService")
public class SysRolePermissionServiceImpl implements SysRolePermissionService {
    @Resource
    private SysRolePermissionDao sysRolePermissionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SysRolePermission queryById(String id) {
        return this.sysRolePermissionDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param sysRolePermission 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<SysRolePermission> queryByPage(SysRolePermission sysRolePermission, PageRequest pageRequest) {
        long total = this.sysRolePermissionDao.count(sysRolePermission);
        return new PageImpl<>(this.sysRolePermissionDao.queryAllByLimit(sysRolePermission, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param sysRolePermission 实例对象
     * @return 实例对象
     */
    @Override
    public SysRolePermission insert(SysRolePermission sysRolePermission) {
        this.sysRolePermissionDao.insert(sysRolePermission);
        return sysRolePermission;
    }

    /**
     * 修改数据
     *
     * @param sysRolePermission 实例对象
     * @return 实例对象
     */
    @Override
    public SysRolePermission update(SysRolePermission sysRolePermission) {
        this.sysRolePermissionDao.update(sysRolePermission);
        return this.queryById(sysRolePermission.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.sysRolePermissionDao.deleteById(id) > 0;
    }


    /**
     * 新增角色权限关联数据
     * @param operationReqVO
     */

    @Override
    public void addRolePermission(RolePermissionOperationReqVO operationReqVO) {
        List<String> permissionIds = operationReqVO.getPermissionIds();
        if(CollectionUtils.isEmpty(permissionIds)){
            return;
        }
        List<SysRolePermission> rolePermissionList = permissionIds.stream().map(permissionId -> {
            SysRolePermission sysRolePermission = new SysRolePermission();
            sysRolePermission.setId(UUID.randomUUID().toString());
            sysRolePermission.setRoleId(operationReqVO.getRoleId());
            sysRolePermission.setPermissionId(permissionId);
            sysRolePermission.setCreateTime(new Date());
            return sysRolePermission;
        }).collect(Collectors.toList());

        sysRolePermissionDao.insertBatch(rolePermissionList);
    }

    @Override
    public List<String> getPermissionIdsByRoleIds(List<String> roleIds) {

        return sysRolePermissionDao.getPermissionIdsByRoleIds(roleIds);
    }


    @Override
    public List<String> getRoleIdsByPermissionId(String permissionId) {
        return sysRolePermissionDao.getRoleIdsByPermissionId(permissionId);
    }
}
